package com.hmdp.config;

import com.hmdp.utils.LoginInterceptor;
import com.hmdp.utils.RefreshTokenInterceptor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class mvcConfig implements WebMvcConfigurer {

    // 通过构造函数注入Spring管理的拦截器
    @Autowired  // 显式注入
    private LoginInterceptor loginInterceptor;

    @Autowired
    private RefreshTokenInterceptor refreshTokenInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry interceptorRegistry){

        // 先添加的拦截器后执行（顺序很重要！）
        interceptorRegistry.addInterceptor(refreshTokenInterceptor)
                .addPathPatterns("/**");  // 全局刷新Token

        // 拦截器 排除特定的url访问拦截
        interceptorRegistry.addInterceptor(loginInterceptor).excludePathPatterns(
                "/user/code",
                "/user/login",
                "/blog/hot",
                "/shop/**",
                "/shop-type/**",
                "/upload/**",
                "/voucher/**",
                // knife4j接口文档请求
                "/doc.html",
                "/webjars/**",
                "/swagger-resources",
                "/v2/api-docs"
        );

    }
}
